package com.example.mytestdemo.view.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

public class RectFaceView extends View {

    private Paint mPaint;
    private final List<DrawInfo> mDrawInfo = new CopyOnWriteArrayList<>();

    public RectFaceView(Context context) {
        super(context);
        init(context);
    }

    public RectFaceView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public RectFaceView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        for (DrawInfo drawInfo : mDrawInfo) {
            draw(canvas, drawInfo);
        }
    }


    private void init(Context context) {
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(Color.GREEN);
        mPaint.setStrokeWidth(5);
    }


    private void draw(Canvas canvas, DrawInfo drawInfo) {
        if (drawInfo == null) {
            return;
        }

        if (drawInfo.getRect() != null) {

            Path path = new Path();
            Rect rect = drawInfo.getRect();

            //左上；
            path.moveTo(rect.left, rect.top + rect.height() / 4);
            path.lineTo(rect.left, rect.top);
            path.lineTo(rect.left + rect.width() / 4, rect.top);

            // 右上
            path.moveTo(rect.right - rect.width() / 4, rect.top);
            path.lineTo(rect.right, rect.top);
            path.lineTo(rect.right, rect.top + rect.height() / 4);

            // 右下
            path.moveTo(rect.right, rect.bottom - rect.height() / 4);
            path.lineTo(rect.right, rect.bottom);
            path.lineTo(rect.right - rect.width() / 4, rect.bottom);

            // 左下
            path.moveTo(rect.left + rect.width() / 4, rect.bottom);
            path.lineTo(rect.left, rect.bottom);
            path.lineTo(rect.left, rect.bottom - rect.height() / 4);

            canvas.drawPath(path, mPaint);
        }
    }


    public void addDrawInfo(DrawInfo drawInfo) {
        if (!mDrawInfo.contains(drawInfo)) {
            mDrawInfo.add(drawInfo);
        }
        postInvalidate();
    }

    public void addDrawInfoList(List<DrawInfo> drawInfos) {
        mDrawInfo.clear();
        mDrawInfo.addAll(drawInfos);
        postInvalidate();
    }

    public void clearDrawInfo() {
        mDrawInfo.clear();
        postInvalidate();
    }


    public static class DrawInfo {

        private int sex;
        private int age;
        private int name;
        private Rect rect;
        private int liveness;

        public int getSex() {
            return sex;
        }

        public void setSex(int sex) {
            this.sex = sex;
        }

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }

        public int getName() {
            return name;
        }

        public void setName(int name) {
            this.name = name;
        }

        public Rect getRect() {
            return rect;
        }

        public void setRect(Rect rect) {
            this.rect = rect;
        }

        public int getLiveness() {
            return liveness;
        }

        public void setLiveness(int liveness) {
            this.liveness = liveness;
        }
    }
}
